<?php
namespace App\Http\Controllers;

use App\Http\Model\FormatCheck;
use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\ConfigServices;
use App\Http\Services\MoonServices;
use App\Http\Services\RegisterServices;
use App\Http\Services\UserServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

//中秋
class MoonController extends Controller
{

    protected $configServices;

    public function __construct()
    {
        parent::__construct();
        //监听充值
        $this->configServices = new ConfigServices();
        $this->userServices = new UserServices();
        $this->registerService = new RegisterServices();
        $this->moonServices = new MoonServices();
        $this->userServices = new UserServices();
    }


    public function assignAction($body)
    {
        if ($body['consumer_type'] == 1) {
            $body_date = $body['create'];
        } else if ($body['consumer_type'] == 2) {
            $body_date = $body['pay_timestamp'];
        } else {
            $body_date = $body['change_timestamp'];
        }

        $consumer_date = $body_date ?? time();
        $config = $this->configServices->getConfig(5);
        $checkDate = $this->configServices->checkConfigDate($config['moon_date'], $consumer_date);
        if (!$checkDate) {
            write_log('moonConsumer', 'timeError', $body);//活动时间错误
            return false;
        }
        if ($body['consumer_type'] == 1) {
            $this->listenRoomGift($body);
        }
        if ($body['consumer_type'] == 2) {
            $this->listenUserRecharge($body);
        }
        if ($body['consumer_type'] == 3) {
            //用户购买中秋礼包
            $this->listenUserPayMoonGift($body);
        }
        return true;
    }

    //监听充值
    public function listenUserRecharge($body)
    {
        $user_id = $body['user_id'] ?? 0;
        $money = $body['price'] / 100 ?? 0;//正式
//        $money = 300;//测试
        $now_date = $body['pay_timestamp'] ?? time();
        //查看用户是否可以赠送玉兔
        $this->moonServices->checkUserRabbit($user_id);
        if ($money < 30) {
            write_log('moonConsumer', 'rechargeMoneyError', $body);//充值金额不达标
            return false;
        }
        //获取当天用户充值记录
        $now_log = $this->moonServices->getUserLog($user_id, date('Y-m-d', $now_date), $body);
        if (!$now_log) {
            write_log('moonConsumer', 'rechargeUserLogError', $body);//用户充值记录添加失败
            return false;
        }
        write_log('moonConsumer', 'rechargeUserLogSuccess', $body);//用户充值记录添加失败
        return true;
    }

    //监听房间送礼 排行版
    public function listenRoomGift($body)
    {
        $res = $this->moonServices->roomRank($body);
        if (!$res) {
            write_log('moonConsumer', 'giftGiveError', $body);//用户充值记录添加失败
            return false;
        }
        write_log('moonConsumer', 'giftGiveSuccess', $body);//用户充值记录添加失败
    }


    //购买中秋礼包
    public function listenUserPayMoonGift($body)
    {
        $user_id = $body['user_id'] ?? 1;
        $num = $body['num'] ?? 0;
        $res = $this->moonServices->payMoonGift($user_id, $num);
        if (!$res) {
            write_log('moonConsumer', 'payMoonGiftError', $body);//用户充值记录添加失败
            return false;
        }
        write_log('moonConsumer', 'payMoonGiftSuccess', $body);//用户充值记录添加失败
        return true;
    }

    //用户礼包领取列表
    public function userGiftList(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $checkUser = $this->userServices->getKkaudioUserInfo($user_id);
        if (!$checkUser) {
            OutJson::toJson(-1, '用户信息错误');
        }
        $res = $this->moonServices->getUserGift($user_id);
        if (!$res) {
            OutJson::toJson(-1, '无用户充值记录');
        }
        OutJson::toJson(200, '成功', $res);
    }

    //点击领取
    public function clickGiftBag(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $checkUser = $this->userServices->getKkaudioUserInfo($user_id);
        if (!$checkUser) {
            OutJson::toJson(-1, '用户信息错误');
        }
        $rank = $request->input('rank') ?? 1;
        $res = $this->moonServices->clickGift($user_id, $rank);
        OutJson::toJson($res['code'], $res['msg'], $res['gift_log']);
    }

    //邀请注册
    public function inviteRegister(Request $request)
    {
        $phone = $request->post('phone');
        $sms_code = $request->post('code');
        $password = $request->post('password');
        $user_id = $request->post('user_id'); // 主框架 uuid
        if (empty($user_id)) {
            $user_id = 0;
        }
        //邀请用户
        if (!$phone || !FormatCheck::checkPhone($phone)) {
            OutJson::toJson(-1, '手机号格式错误');
        }
        if (!$sms_code || !$password) {
            OutJson::toJson(-1, '参数错误');
        }
        if (!FormatCheck::checkPassword($password)) {
            OutJson::toJson(-1, '密码长度必须由6-16位数字字母组成，不能包含特殊字符');
        }
//        审核通过 传输register api
        $register = $this->moonServices->send_register($phone, $sms_code, $password, $user_id);
        if ($register['code'] != 200) {
            OutJson::toJson($register['code'], $register['msg'], $register['data']);
        }
        $new_uid = $register['data']['user_id'];//新用户id
        if ($user_id == 0) {
            write_log('moonConsumer', 'noInviteUid', $new_uid);
            OutJson::toJson(200);
        }
        //判断注册是否过期
        $config = $this->configServices->getConfig(5);
        $checkDate = $this->configServices->checkConfigDate($config['moon_date'], time());
        if (!$checkDate) {
            write_log('moonConsumer', 'inviteUserTimeError', $user_id);//活动时间错误
            OutJson::toJson(200);
        }
        //注册成功 绑定用户关系
        $createUser = $this->moonServices->createInviteUser($new_uid, $user_id);
        if (!$createUser) {
            //记录日志
            write_log('moonConsumer', 'createUserError', $new_uid);
            //api入库失败
            OutJson::toJson(401, '用户生成失败');
        }
//        注册完毕
        OutJson::toJson(200);
    }

    //兑换
    public function convertFowl(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $type = $request->input('type') ?? 1; //1 亲密值 2名片
        $num = abs($request->input('num')) ?? 1; //输入数量
        if ($type == 2) {
            $num = 20;
        }
        $res = $this->moonServices->convert($user_id, $type, $num);
        OutJson::toJson($res['code'], $res['msg']);
    }

    //获取玉兔数量
    public function getUserRabbit(Request $request)
    {
        $user_id = $request->input('user_id');
        $res = $this->moonServices->getUserRabbitNum($user_id);
        OutJson::toJson(200, '成功', $res);
    }

    public function dialTicket(Request $request)
    {
        $user_id = $request->input('user_id') ?? '0';
        $ticket_num = $request->input('ticket_num') ?? 1;
        $res = $this->moonServices->checkTicket($user_id, $ticket_num);
        OutJson::toJson($res['code'], $res['msg'], $res['data']);
    }

    public function getDialLog(Request $request)
    {
        $user_id = $request->input('user_id') ?? '0';
        $res = $this->moonServices->getDialAwardLog($user_id);
        OutJson::toJson(200, '成功', $res);
    }

    public function getUserTicket(Request $request)
    {
        $user_id = $request->input('user_id') ?? '0';
        $res = $this->moonServices->getUserTicketNum($user_id);
        $diamond_value = $this->userServices->getUserDiamond($user_id);
        $res['user_diamond'] = $diamond_value['diamond_balance'] ?? 0;
        OutJson::toJson(200, '成功', $res);
    }

    public function getUserRank(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $res = $this->moonServices->giftRank($user_id);
        OutJson::toJson(200, '成功', $res);
    }

    public function awardGift()
    {
        return $this->moonServices->awardGift();
    }

    public function getRoomRankList()
    {
        return $this->moonServices->room_rank_list();
    }

    public function getUserRankList()
    {
        return $this->moonServices->userRankList();
    }


    public function rank()
    {
        return $this->configServices;
    }

    public function createNewCacheKey()
    {
        return $this->moonServices->createNewCacheKey();

    }

}